package com.example.mybatisplus.common.utls;

import com.example.mybatisplus.model.domain.Admin;
import com.example.mybatisplus.model.domain.WhitelistSetting;
import com.example.mybatisplus.model.dto.UserInfoDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SecurityUtils {
    /**
     * 获取当前用户
     *
     * @return
     */
    public static Admin getCurrentUserInfo() {
        Admin userInfo = SessionUtils.getCurrentUserInfo();
        //模拟登录
        if (userInfo == null) {
            userInfo = new Admin();
            userInfo.setLoginName("模拟");
        }

        return userInfo;
    }

    public static UserInfoDTO getUserInfo() {
        // 从session中获取当前用户
        // 有值 表示登录过
        // 没有值  表示没有登陆过 或者登陆过期  获取不到登陆用户
        WhitelistSetting currentUser = SessionUtils.getCurrentUser();

        UserInfoDTO userInfoDTO = new UserInfoDTO();
        if(currentUser!=null){
            userInfoDTO.setId(currentUser.getId());
            userInfoDTO.setName(currentUser.getName());
            userInfoDTO.setUserType(currentUser.getRoleId());
        }
        return userInfoDTO;
    }
}
